
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.tompai.common.lock;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

/**
* @desc: tompai-common
* @name: WriteThread.java
* @author: tompai
* @email：liinux@qq.com
* @createTime: 2021年2月7日 下午11:41:29
* @history:
* @version: v1.0
*/

public class WriteThread extends Thread {

	private String sourceFile;
	private String targetFile;
	private String threadName;

	public WriteThread(String sourceFile, String targetFile, String ThreadName) {
		this.sourceFile = sourceFile;
		this.targetFile = targetFile;
		this.threadName = ThreadName;
	}

	@Override
	public void run() {
		RandomAccessFile raf = null;
		FileChannel fc = null;
		FileLock fl = null;
		FileInputStream in = null;
		try {
			raf = new RandomAccessFile(targetFile, "rw");
			fc = raf.getChannel();
			while (true) {
				try {
					fl = fc.tryLock();
					System.out.println(fl.isShared());
					System.out.println(this.threadName + " : get the lock");
					try {
						sleep(1000);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					break;
				} catch (Exception e) {
					System.out.println(this.threadName + " is block");
					try {
						sleep(1000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
				}

			}
			in = new FileInputStream(sourceFile);
			byte[] b = new byte[1024];
			int len = 0;
			ByteBuffer bb = ByteBuffer.allocate(1024);
			while ((len = in.read(b)) != -1) {
				bb.clear();
				bb.put(b, 0, len);
				bb.flip();
				fc.write(bb);

			}
			System.out.println(this.threadName + " : write success");
			fl.release();
			System.out.println(this.threadName + " : release lock");
			raf.close();
			fc.close();
			in.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			System.out.println(this.threadName + " : write failed");
		} finally {
			if (fl != null && fl.isValid()) {
				try {
					fl.release();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
